<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader :arr="list" @create="add"></TodoHeader>
    <TodoMain :arr="showList" @delFn="del"></TodoMain>
    <TodoFooter :arr="list" @changType="change" @clearFn="clear"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: [
        { id: 100, name: "吃饭", isDone: true },
        { id: 101, name: "睡觉", isDone: false },
        { id: 102, name: "打豆豆", isDone: true },
      ],
      getSel: 'all'
    };
  },
  methods: {
    del(id) {
      console.log(id);
      const index = this.list.findIndex(item => item.id === id)
      this.list.splice(index, 1)
    },
    add(content) {
      this.list.push({
        id: this.list.length === 0 ? 100 : this.list.at(-1).id + 1,
        name: content,
        isDone: false,
      })
    },
    change(isSel) {
      this.getSel = isSel
    },
    clear(){
      this.list = this.list.filter(item =>item.isDone !== true)
    }
  },
  computed: {
    showList(){
      if (this.getSel === 'yes') {
        return this.list.filter(item => item.isDone)
      } else if (this.getSel === 'no') {
        return this.list.filter(item => !item.isDone)
      } else {
        return this.list
      }
    }
  },

};
</script>